/***
    Amrita ITEWS - Copyright (C) 2009 Amrita Vishwa Vidyapeetham, Amritapuri.
                                       (http://www.amrita.edu)
    ***************************************************************************
    This file is part of the Amrita ITEWS distribution.
    Amrita ITEWS is free software; you can redistribute it and/or modify it
    under the terms of the GNU General Public License (version 2) as published
    by the Free Software Foundation AND MODIFIED BY the Amrita ITEWS exception.
    ***NOTE*** The exception to the GPL is included to allow you to distribute
    a combined work that includes Amrita ITEWS without being obliged to provide
    the source code for proprietary components outside of the Amrita ITEWS
    software. Amrita ITEWS is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
    Public License for more details. You should have received a copy of the GNU
    General Public License and the Amrita ITEWS license exception along with
    Amrita ITEWS if not then it can be viewed here:
    http://itews.amrita.ac.in/license.html.


    Documentation, latest information, license and contact details are at:
    http://itews.amrita.ac.in/


    Amrita ITEWS source code can be found at:
    http://code.google.com/p/itews


    The initial code-base of Amrita ITEWS was developed by Amrita Vishwa
    Vidyapeetham as part of the project titled,"Intelligent & Interactive
    Telematics using Emerging Wireless Technologies for Transport Systems
    (ITEWS)" sponsored by Technology Information Forecasting and Assessment
    Council (TIFAC), India.
***/
#include <typedefs.h>
#include <FreeRTOS.h>
#include <eventq.h>
#include <event.h>
#include <stream_eventq.h>
#include <uip_task.h>
#include <errno.h>
#include <task.h>
#include <mutex_api.h>
#include <timer.h>
#include <socket.h>

#define INVALID_SOCKET  0xffff

#define CTX_GET_PORT()      ((uint16) (e_ctx->ctx_data>>16))
#define CTX_GET_SOCKET()    ((int32)  (e_ctx->ctx_data&0xffff))
#define CTX_SOCKET_IS_VALID() ((e_ctx->ctx_data&0xffff) != 0xffff)
#define CTX_PORT_IS_VALID()   (CTX_GET_PORT() != 0)

#define CTX_SET_PORT(p)\
        e_ctx->ctx_data&=0xffff;\
        e_ctx->ctx_data|=( ((uint32)p)<<16)

#define CTX_SET_SOCKET(s)\
        e_ctx->ctx_data&=0xffff0000;\
        e_ctx->ctx_data|=(((uint32)s)&0xffff);

/*Stream EventQ Operations Structure Variable Initialization.*/
eventq_ops_t stream_eventq_ops = {
                                stream_eventq_open,
                                stream_eventq_close,
                                stream_eventq_sync_get,
                                stream_eventq_sync_put,
                                0,
                                0
                            };

STATUS stream_eventq_open( eventq_ctx_t *e_ctx, const char *eventq_name )
{
    int32 status;

    CTX_SET_SOCKET(INVALID_SOCKET);
    CTX_SET_PORT(0);

    status = _socket_open();

    if(status >= 0)//this is a socket fd
    {
        CTX_SET_SOCKET(status);
    }

    return status;
}

STATUS stream_eventq_close( eventq_ctx_t *e_ctx )
{
    int32 status;

    status = _socket_close(CTX_GET_SOCKET());
    CTX_SET_SOCKET(-1);

    return status;
}

STATUS stream_eventq_sync_put( eventq_ctx_t *e_ctx, event_t *e, HRESULT *res )
{
    int32 status=-ENOTSUP;

    if(!CTX_SOCKET_IS_VALID())
    {
        return -EBADFD;
    }

    switch(e->type)
    {
        case EVT_STREAM_BIND:
            CTX_SET_PORT(((stream_param_t*)e->param)->port);
            status = _socket_bind(CTX_GET_SOCKET(), CTX_GET_PORT());
            break;
        case EVT_STREAM_LISTEN:
            status = _socket_listen(CTX_GET_SOCKET());
            break;
        case EVT_STREAM_CONNECT:
            status = _socket_connect(CTX_GET_SOCKET(), e->param);
            break;
        case EVT_STREAM_PUT_DATA:
            status = _socket_write(CTX_GET_SOCKET()
                                , e->buf, e->ndata);
            break;
        case EVT_STREAM_SET_OPT:
            status = _socket_setopt(CTX_GET_SOCKET()
                                , (int32)e->param, e->buf);
            break;

        case EVT_STREAM_CLOSE:
            if(CTX_SOCKET_IS_VALID())
            {
                status = _socket_close(CTX_GET_SOCKET());
                CTX_SET_SOCKET(INVALID_SOCKET);
            }else
            {
                status = -EBADFD;
            }
            break;

        default:
            break;
    }

    return status;
}

STATUS stream_eventq_sync_get( eventq_ctx_t *e_ctx, event_t *e )
{
    int32 status=-ENOTSUP;
    stream_param_t *param;

    if((e->type != EVT_STREAM_ACCEPT) && !CTX_SOCKET_IS_VALID())
    {
        return -EBADFD;
    }

    switch(e->type)
    {
        case EVT_STREAM_ACCEPT:
            status=0;
            if(!CTX_SOCKET_IS_VALID())
            {
                if(CTX_PORT_IS_VALID())
                {
                    status = _socket_open();
                    if(status >=0)
                    {
                        CTX_SET_SOCKET(status);
                        status = _socket_bind(CTX_GET_SOCKET(),
                                        CTX_GET_PORT());
                        if(status >= 0)
                        {
                            _socket_listen(CTX_GET_SOCKET());
                        }
                    }
                }else
                {
                    status = -EINVAL;
                }
            }
            if(status >= 0)
            {
                status = _socket_accept(CTX_GET_SOCKET(), e->param);
                param = (stream_param_t*)e->param;
                param->conn_fd = CTX_GET_SOCKET();//for consistency - we don't use this
            }
            break;
        case EVT_STREAM_GET_DATA:
            status = _socket_read(CTX_GET_SOCKET(), e->buf, e->ndata);
            break;
        default:
            break;
    }
    return status;
}
